package com.itheima.controller;


import com.itheima.common.PaginationVO;
import com.itheima.common.Result;
import com.itheima.entity.YddCate;
import com.itheima.service.ContentService;
import com.itheima.vo.contentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class ContentController {
    private final ContentService contentService;

    public ContentController(ContentService contentService) {
        this.contentService = contentService;
    }

    /**
     * 获取所有内容
     *
     * @return {@link Result}<{@link List}<{@link YddCate}>>
     */
    @GetMapping("/all")
    public Result<List<contentVO>> getAllContent() {
        log.info("查询全部内容");
        List<contentVO> cate = contentService.getAllContent();
        return Result.success(cate);
    }

    /**
     * del含量
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/del")
    public Result<?> delContent(String id) {
        log.info("删除内容{}", id);
        contentService.delContent(id);
        return Result.success();
    }

    /**
     * 行信息内容
     *
     * @param id id
     * @return {@link Result}<{@link contentVO}>
     */
    @GetMapping("/rowInfo")
    public Result<contentVO> rowInfoContent(String id) {
        log.info("删除内容{}", id);
        contentVO cv = contentService.rowInfoContent(id);
        return Result.success(cv);
    }

    /**
     * 获取页面内容
     *
     * @param catename 连环
     * @param page     页
     * @param limit    限度
     * @return {@link Result}<{@link PaginationVO}<{@link contentVO}>>
     */
    @GetMapping("/list")
    public Result<PaginationVO<contentVO>> getPageContent(String catename,
                                                  @RequestParam(defaultValue = "1") String page,
                                                  @RequestParam(defaultValue = "10") String limit) {
        log.info("分页查询全部内容:{},{},{}", catename, page, limit);
        PaginationVO<contentVO> pages = contentService.getPageContent(catename, page, limit);
        return Result.success(pages);
    }

    /**
     * 添加内容
     *
     * @param cv cv
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/add")
    public Result<?> addContent(@RequestBody contentVO cv) {
        log.info("添加内容:{}",cv);
        contentService.addContent(cv);
        return Result.success();
    }

    /**
     * 更新内容
     *
     * @param cv cv
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/update")
    public Result<?> updateContent(@RequestBody contentVO cv) {
        log.info("添加内容:{}", cv);
        contentService.updateContent(cv);
        return Result.success();
    }
}
